<template>
  <view class="vehicle-verify-page page-content">
    <z-paging ref="paging" v-model="currentList" @query="queryList" :auto="false" :refresher-enabled="true"
      :loading-more-enabled="true" empty-view-text="暂无数据">
      <template #top>
        <up-navbar title="车位监控列表" :autoBack="false" :fixed="true" bgColor="transparent" :border="false" placeholder
          @leftClick="handleBackClick">
          <template>
            <view></view>
          </template>
        </up-navbar>

        <up-sticky class="padding-20" bgColor="transparent">
          <!-- 数据统计，包含总泊位数、空余数、禁用数、已登记订单数、未登记订单数、人工取证订单数 -->
          <view class="statistics-container">
            <!-- 泊位统计卡片 -->
            <view class="stat-card parking-spaces">
              <view class="card-header">
                <view class="header-icon">
                  <up-icon name="car" size="24" color="#4080FF"></up-icon>
                </view>
                <text class="header-title">泊位统计</text>
              </view>
              <view class="card-content">
                <view class="stat-item">
                  <text class="stat-value">100</text>
                  <text class="stat-label">总泊位数</text>
                </view>
                <view class="stat-item">
                  <text class="stat-value highlight-green">10</text>
                  <text class="stat-label">空余数</text>
                </view>
                <view class="stat-item">
                  <text class="stat-value highlight-gray">10</text>
                  <text class="stat-label">禁用数</text>
                </view>
              </view>
            </view>
            
            <!-- 订单统计卡片 -->
            <view class="stat-card orders">
              <view class="card-header">
                <view class="header-icon">
                  <up-icon name="file-text" size="24" color="#FF7D00"></up-icon>
                </view>
                <text class="header-title">订单统计</text>
              </view>
              <view class="card-content">
                <view class="stat-item">
                  <text class="stat-value highlight-blue">10</text>
                  <text class="stat-label">已登记订单</text>
                </view>
                <view class="stat-item">
                  <text class="stat-value highlight-orange">10</text>
                  <text class="stat-label">未登记订单</text>
                </view>
                <view class="stat-item">
                  <text class="stat-value highlight-purple">10</text>
                  <text class="stat-label">人工取证订单</text>
                </view>
              </view>
            </view>
          </view>

          <view class="header-bar">
            <view class="header-bar-location" @click="showTimePicker = true">
              <text class="title">区域</text>
              <up-icon name="arrow-down"></up-icon>
            </view>
            <view class="header-bar-location" @click="showTimePicker = true">
              <text class="title">停车场</text>
              <up-icon name="arrow-down"></up-icon>
            </view>
            <up-picker :show="showTimePicker" :columns="columns" :closeOnClickOverlay="true"
              @confirm="showTimePicker = false" @cancel="showTimePicker = false"
              @close="showTimePicker = false"></up-picker>
          </view>

          <up-search :clearabled="true" :showAction="false" placeholder="请输入泊位号"></up-search>
          <up-tabs :list="tabsList" :scrollable="false" @change="handleTabChange"></up-tabs>
        </up-sticky>
      </template>

      <view class="vehicle-verify-list">
        <view class="list-item-card" v-for="(item, index) in currentList" :key="index">
          <view class="card-header">
            <view class="title">
              泊位号：<text class="number">1001</text>
            </view>
          </view>
          <view class="card-content-box">
            <view class="content">
              <view class="item">
                <text class="label">
                  状态
                </text>
                <view class="value">
                  <view class="item-tag">
                    <up-tag text="粤ET5G22" type="success"></up-tag>
                  </view>
                  <view class="item-tag">
                    <up-tag text="临时车" type="warning"></up-tag>
                  </view>
                </view>
              </view>
              <view class="item">
                <text class="label">
                  停车场：
                </text>
                <text class="value">
                  盈月路蘑菇蹲
                </text>
              </view>
              <view class="item">
                <text class="label">
                  订单编号：
                </text>
                <text class="value">
                  750185240005316609
                </text>
              </view>
              <view class="item">
                <text class="label">
                  本次计费：
                </text>
                <text class="value">
                  100元
                </text>
              </view>
              <view class="item">
                <text class="label">
                  历史欠费：
                </text>
                <text class="value">
                  156元
                </text>
              </view>
              <view class="item">
                <text class="label">
                  停车时长：
                </text>
                <text class="value">
                  1天05:30:00
                </text>
              </view>
            </view>
            <!-- <view class="card-footer">
              <view class="checkbox-box">
              </view>
              <view class="operate">
                <up-button type="primary" text="编辑订单" plain class="custom-style" @click="handleEdit(item)"></up-button>
                <up-button type="primary" text="结束订单" class="custom-style" @click="handleEndOrder(item)"></up-button>
              </view>
            </view> -->
          </view>
        </view>
      </view>
    </z-paging>

    <ParkingOrderModal v-model:show="show" :type="type" :row-data="editingData" />
    <up-modal :show="showDelModal" contentTextAlign="center" showCancelButton closeOnClickOverlay title="提示"
      content='是否确认结束订单？' @cancel="closeDelModal" @confirm="closeDelModal" @close="closeDelModal"></up-modal>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { getRealNameVerifyList } from '@/api';
import uniDatetimePicker from '@/components/uni-datetime-picker/uni-datetime-picker.vue'
import { formatTime } from '@/utils/tools';

const { bool: show, setTrue: openModal } = useBoolean();
const { bool: showDelModal, setTrue: openDelModal, setFalse: closeDelModal } = useBoolean();
const type = ref('add')
const editingData = ref({})

// 选项卡数据  
const tabsList = [
  { name: '全部' },
  { name: '待支付' },
  { name: '已支付' },
  { name: '无需支付' },
  { name: '欠费' },
];
const currentTab = ref(0);
const showTimePicker = ref(false);
const columns = reactive([
  ['近7天', '近1个月', '近3个月', '近6个月', '近1年'],
]);
// z-paging 组件引用
const paging = ref(null)

// 当前显示的列表数据
const currentList = ref([])

// 获取认证状态文本
const getStatusText = (status) => {
  switch (status) {
    case 1:
      return '待审核'
    case 2:
      return '审核通过'
    case 3:
      return '审核驳回'
    default:
      return '未知'
  }
}

const handleAdd = () => {
  type.value = 'add'
  openModal()
}

const handleEdit = (item: any) => {
  type.value = 'edit'
  editingData.value = {
    carNumber: '粤XZ5168',
    modifyTime: Date.now(),
  }
  openModal()
}

const handleEndOrder = (item: any) => {
  openDelModal()
}

// 选项卡切换事件
const handleTabChange = (e) => {
  const newIndex = e.index;
  if (currentTab.value === newIndex) {
    return; // 如果是同一个tab，不需要重新加载
  }

  currentTab.value = newIndex;

  // 切换tab时重新加载数据
  if (paging.value) {
    paging.value.reload();
  }
};

// 处理返回点击事件
const handleBackClick = () => {
  uni.redirectTo({
    url: '/pages/home/index'
  })
}


// 获取车辆列表数据
const queryList = async (pageNo, pageSize) => {
  console.log(pageNo, pageSize, currentTab.value)

  const params = {
    pageNum: pageNo,
    pageSize
  }

  await fetchList(params)
};

const fetchList = async (params) => {
  try {
    const result: any = await getRealNameVerifyList({ ...params, status: currentTab.value === 0 ? undefined : currentTab.value })
    const data = result.data
    paging.value.complete(data.records || [])
  } catch (error) {
    paging.value.complete(false)
  }
}


// 搜索按钮点击事件
const handleSearch = () => {
};

// 查看详情
const handleView = (row: any) => {
  // 跳转到详情页逻辑
  console.log('查看详情:', row);
};

// 审核操作
const handleVerify = (row: any) => {
  // 跳转到审核页逻辑
  console.log('审核操作:', row);
};

// 页面挂载时获取数据
onMounted(() => {
  fetchList({
    pageNum: 1,
    pageSize: 10,
  })
});
</script>

<style scoped lang="scss">
.vehicle-verify-list {
  padding: 20px;
}

.header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0rpx 20rpx 30rpx;

  .time-picker {
    width: 480rpx;
    display: flex;
    align-items: center;

    .title {
      font-size: 28rpx;
      font-weight: bold;
      color: #333;
      margin-right: 10rpx;
    }
  }

  &-location {
    display: flex;
    flex: 1;
    justify-content: center;
    align-items: center;

    .location-icon {
      width: 40rpx;
      height: 40rpx;
      margin-right: 10rpx;
    }

    .title {
      font-size: 28rpx;
      font-weight: bold;
      color: #333;
      margin-right: 10rpx;
    }

    .arrow-icon {
      width: 30rpx;
      height: 30rpx;
    }
  }

  .waiting-count {
    font-weight: 500;
    font-size: 24rpx;
    color: #333333;
    display: flex;
    align-items: center;

    text {
      font-weight: bold;
      font-size: 36rpx;
      color: #fe6000;
      margin-left: 10rpx;
    }
  }
}

/* 数据统计样式 */
.statistics-container {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
  padding: 10rpx 0 30rpx;
}

.stat-card {
  background-color: #FFFFFF;
  border-radius: 16rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  padding: 24rpx;
  overflow: hidden;
  position: relative;
  
  .card-header {
    display: flex;
    align-items: center;
    margin-bottom: 20rpx;
    
    .header-icon {
      width: 48rpx;
      height: 48rpx;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 16rpx;
    }
    
    .header-title {
      font-size: 30rpx;
      font-weight: 600;
      color: #333333;
    }
  }
  
  .card-content {
    display: flex;
    justify-content: space-between;
  }
  
  .stat-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex: 1;
    
    .stat-value {
      font-size: 40rpx;
      font-weight: bold;
      color: #333333;
      line-height: 1.2;
      margin-bottom: 8rpx;
    }
    
    .stat-label {
      font-size: 24rpx;
      color: #666666;
    }
  }
}

/* 泊位统计卡片特殊样式 */
.parking-spaces {
  border-left: 8rpx solid #4080FF;
  
  .header-icon {
    background-color: rgba(64, 128, 255, 0.1);
  }
}

/* 订单统计卡片特殊样式 */
.orders {
  border-left: 8rpx solid #FF7D00;
  
  .header-icon {
    background-color: rgba(255, 125, 0, 0.1);
  }
}

/* 高亮颜色 */
.highlight-green {
  color: #00C48C !important;
}

.highlight-gray {
  color: #909399 !important;
}

.highlight-blue {
  color: #4080FF !important;
}

.highlight-orange {
  color: #FF7D00 !important;
}

.highlight-purple {
  color: #722ED1 !important;
}

.search-bar {
  margin-bottom: 20px;
  display: flex;
  gap: 10px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>